package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out = response.getWriter();


		conn cn=new conn();
		String f = request.getParameter("f");

		if(f==null)
		{
		
			String lname=request.getParameter("txtUserName");
			String pwd=request.getParameter("txtPassWord");

		
		
			String sql="select * from admin where lname='"+lname+"' and pwd='"+pwd+"' ";
		
			ResultSet rs=cn.query(sql);
	
			try {
				if(rs.next()){
					HttpSession session = request.getSession();
					session.setAttribute("adminId", rs.getString("aid"));
					session.setAttribute("adminLname", rs.getString("lname"));
					session.setAttribute("role", rs.getString("sf"));
					response.sendRedirect("admin/index.jsp");
				}
				else
				{
					out.println("<script>");
					out.println("alert('对不起，用户名或密码错误！');");
					out.println("window.location='login.jsp'");
					out.println("</script>");
				}
					
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


		
		
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
